//: src/com/silvtar/rsync/SSRsyncd.java
/* 
 * Created by 朴晟宁 <piaosn@gmail.com> on 5/25/2013. 
 * Copyright (c) 2013 朴晟宁. All rights reserved.
 * 
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program;
 * 
 * *********************************************************************
 * 
 * Modify History:
 * Date        Version    Description
 * 5/25/2013   0.1        Create the class. 
 */

package com.silvtar.rsync;

import com.silvtar.io.StdOut;
import com.silvtar.io.StreamToolkit;
import com.silvtar.util.Bin;

/**
 * rsync守护进程打包类 本类会根据需要同步的文件夹自动生成rsyncd配置文件。并同步。
 * 
 * @author 朴晟宁/Silvtar
 * @author piaosn@gmail.com
 * @version 0.1
 */
public class SSRsyncDaemon {

    public static boolean run = false;

    String                rsyncRootPath;
    String                rsyncdConfigPath;

    String                authUsers;
    String                uid;
    String                gid;
    int                   port;
    StreamToolkit         config_ST;

    
    /**
     * 关闭本类用到的流
     */
    public void closeCST() {
        config_ST.close();
    }

    /**
     * 开启rsync守护进程
     */
    public void startRsyncDaemon() {

        Bin.rsyncd(rsyncdConfigPath);
        run = true;
    }

    /**
     * 配置rsync daemon。基础设定。
     * 
     * @param authUsers
     * @param uid
     * @param gid
     */
    public void configRsyncDaemon(String authUsers, String uid, String gid,
            String rsyncRootPath, int port) {
        this.authUsers = authUsers;
        this.uid = uid;
        this.gid = gid;
        this.rsyncRootPath = rsyncRootPath;
        this.port = port;
        rsyncdConfigPath = rsyncRootPath + "/rsyncd.conf";

        // 以追加模式打开文件
        config_ST = new StreamToolkit(rsyncdConfigPath, "rw", false);
        config_ST.writeBytes("uid = " + uid + "\n");
        config_ST.writeBytes("gid = " + gid + "\n");
        config_ST.writeBytes("use chroot = " + "no" + "\n");
        config_ST.writeBytes("max connections = " + 4 + "\n");
        config_ST.writeBytes("strict modes = " + "yes" + "\n");
        config_ST.writeBytes("port = " + port + "\n");
        config_ST.writeBytes("pid file = " + rsyncRootPath
                + "/rsyncd_conf/run/rsyncd.pid\n");
        config_ST.writeBytes("log file = " + rsyncRootPath
                + "/rsyncd_conf/log/rsyncd.log\n");
        config_ST.writeBytes("lock file = " + rsyncRootPath
                + "/rsyncd_conf/run/rsync.lock\n");
    }

    /**
     * 向配置文件中写入一个项目
     * 
     * @param path
     */
    public void writeItem(String path) {

        if (config_ST == null) {
            StdOut.printErr("尚未配置rsyncd。启动守护进程失败。请首先调用config函数！");
            return;
        }

        String pathName = path.substring(path.lastIndexOf("/") + 1);

        config_ST.writeBytes("\n\n");
        config_ST.writeBytes("[" + pathName + "]" + "\n");
        config_ST.writeBytes("path = " + path + "\n");
        config_ST.writeBytes("comment = remote folder" + "\n");
        config_ST.writeBytes("auth users = " + authUsers + "\n");
        config_ST.writeBytes("uid = " + uid + "\n");
        config_ST.writeBytes("gid = " + gid + "\n");
        config_ST.writeBytes("secrets file = " + rsyncRootPath
                + "/rsyncd_conf/rsyncd.ps\n");
        config_ST.writeBytes("read only = no" + "\n");
        config_ST.writeBytes("list = no" + "\n");
    }
}
